package br.com.naybee.framework.client.arduino.cliente;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import br.com.naybee.framework.client.models.Model;
import br.com.naybee.framework.client.models.Models;

@Service
public class ClientArduino {

	private Logger logger = LoggerFactory.getLogger(ClientArduino.class);

	private static final String serverIP = "192.168.1.200";
	private static final int serverPort = 3128;
	private Model model;

	public static ClientArduino builder() {
		return new ClientArduino();
	}

	public void initComando() {
		logger.info("*****************************************************************************************************");
		try {
			String msgToServer = "";
			String msgFromServer;

			Socket clientSocket = new Socket(serverIP, serverPort);

			DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

			msgToServer = comandoArduino();

			outToServer.writeBytes(msgToServer + '\n');
			logger.info("sending to Arduino-Server: " + msgToServer);

			msgFromServer = inFromServer.readLine();
			logger.info("recieved from Arduino-Server: " + msgFromServer);

			clientSocket.close();

		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		logger.info("*****************************************************************************************************");
	}

	private String comandoArduino() {
		String msgToServer = "";

		msgToServer = getModel().getStr("comando") + ":" + getModel().getModel("porta").getStr("codigoPorta") + "?";
		logger.info("Comando: " + getModel().getStr("comando") + " Porta: " + getModel().getModel("porta").getStr("codigoPorta"));

		return msgToServer;
	}

	private Model getModel() {
		return this.model;
	}

	public ClientArduino setModel(Model model) {
		this.model = model;
		return this;
	}

	public Model verificarConexaoArduino() {
		logger.info("*****************************************************************************************************");
		try {
			String msgToServer = "";
			String msgFromServer;

			Socket clientSocket = new Socket(serverIP, serverPort);

			DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
			BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

			msgToServer = "testar-conexao$";

			outToServer.writeBytes(msgToServer + '\n');
			logger.info("sending to Arduino-Server: " + msgToServer);

			msgFromServer = inFromServer.readLine();
			logger.info("recieved from Arduino-Server: " + msgFromServer);

			clientSocket.close();

			return Models.newModel().add("retornoArduino", msgFromServer);

		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		logger.info("*****************************************************************************************************");
		return null;
	}

}
